package com.hairline.controller;

import com.hairline.bean.*;

import com.hairline.bean.bo.*;
import com.hairline.bean.vo.BaseRespVo;
import com.hairline.bean.vo.ListVo;
import com.hairline.service.SpreadServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.System;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class SpreadController {

    @Autowired
    ConfigurableConversionService conversionService;

    @Autowired
    SpreadServices spreadServices;

    @RequestMapping("ad/list")
    public BaseRespVo getAdList(AdListBo adListBo) {

        ListVo ads = spreadServices.getAdListPages(adListBo);

        return BaseRespVo.ok(ads);
    }

    @RequestMapping("ad/create")
    public BaseRespVo creatAd(@RequestBody Ad create) {
        Ad newAd = spreadServices.creatAd(create);
        return BaseRespVo.ok(newAd);
    }

    @RequestMapping("ad/update")
    public BaseRespVo uodateAd(@RequestBody Ad update) {

        Ad updateAd = spreadServices.updateAd(update);

        return BaseRespVo.ok(updateAd);
    }

    @RequestMapping("ad/delete")
    public BaseRespVo deleteAd(@RequestBody Ad update) {

        spreadServices.delete(update);
        return BaseRespVo.ok();
    }

    //    ---------------------Coupon相关-------------------------------
    //
    @RequestMapping("coupon/list")
    public BaseRespVo getCouponList(CouponListBo couponListBo) {
        ListVo listVo = spreadServices.getCouponList(couponListBo);
        return BaseRespVo.ok(listVo);
    }


    @RequestMapping("coupon/create")
    public BaseRespVo createCoupon(@RequestBody CouponBo couponBo) {


        if (couponBo.getStartTime() != null && couponBo.getAddTime() != null) {
            if (couponBo.getStartTime().compareTo(couponBo.getEndTime()) > 0) {
                return BaseRespVo.fail("开始时间不能晚于结束时间");
            }
        }

        Coupon coupon = spreadServices.createCoupon(couponBo);
        return BaseRespVo.ok(coupon);
    }

    @RequestMapping("coupon/listuser")
    public BaseRespVo getListuser(CouponUserListBo couponUserListBo) {
        ListVo vo = spreadServices.getListuser(couponUserListBo);

        return BaseRespVo.ok(vo);
    }

    @RequestMapping("coupon/read")
    public BaseRespVo getCouponById(Integer id) {
        Coupon coupon = spreadServices.getCouponById(id);
        return BaseRespVo.ok(coupon);
    }

    @RequestMapping("coupon/update")
    public BaseRespVo updateCoupon(@RequestBody CouponBo updateCoupon) {
        Coupon coupon = spreadServices.updateCoupon(updateCoupon);
        return BaseRespVo.ok(coupon);
    }

    @RequestMapping("coupon/delete")
    public BaseRespVo deleteCoupon(@RequestBody Coupon updateCoupon) {
        spreadServices.deleteCoupon(updateCoupon);
        return BaseRespVo.ok();
    }

    //-----------------topic相关-----------
    @RequestMapping("topic/list")
    public BaseRespVo getTopicList(TopicListBo topicListBo) {

        ListVo vo = spreadServices.getTopicList(topicListBo);
        return BaseRespVo.ok(vo);
    }

    @RequestMapping("topic/create")
    public BaseRespVo createTopic(@RequestBody TopicCreatBo topicCreatBo) {
        System.out.println(111);
        Topic topic = spreadServices.createTopic(topicCreatBo);

        return BaseRespVo.ok(topic);

    }

    @RequestMapping("topic/update")
    public BaseRespVo updateTopic(@RequestBody Topic updateTopic) {
        System.out.println(111);
        Topic topic = spreadServices.updateTopic(updateTopic);

        return BaseRespVo.ok(topic);

    }

    @RequestMapping("topic/delete")
    public BaseRespVo deleteTopic(@RequestBody Topic topic) {

        spreadServices.deleteTopic(topic);

        return BaseRespVo.ok();
    }

    //----------groupon相关----------
    @RequestMapping("groupon/list")
    public BaseRespVo getGrouponRulesList(GroupListBo groupListBo) {
        System.out.println(1);
        ListVo vo = spreadServices.getGrouponList(groupListBo);

        return BaseRespVo.ok(vo);
    }


    @RequestMapping("groupon/create")
    public BaseRespVo createGrouponRules(@RequestBody GrouponRules createGrouponRules) throws ParseException {
        System.out.println(1);
        GrouponRules grouponRules = spreadServices.createGroupon(createGrouponRules);

        return BaseRespVo.ok(grouponRules);
    }


    @RequestMapping("groupon/update")
    public BaseRespVo updateGrouponRules(@RequestBody GrouponRules updateGroupRules) {

        GrouponRules grouponRules = spreadServices.updateGrouponRules(updateGroupRules);

        return BaseRespVo.ok(grouponRules);
    }

    @RequestMapping("groupon/delete")
    public BaseRespVo deleteGroupon(@RequestBody GrouponRules deleteGrouponRules) {

        spreadServices.deleteGroupon(deleteGrouponRules);

        return BaseRespVo.ok();
    }


    @RequestMapping("groupon/listRecord")
    public BaseRespVo getGrouponListRecord(GrouponListRecordBo grouponListRecordBo) {

        ListVo vo = spreadServices.getGrouponListRecord(grouponListRecordBo);
        return BaseRespVo.ok();
    }
}
